<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="icon" href="/favicon.ico">
    <link rel="stylesheet" href="/resources/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="/resources/css/public.css" media="all" />
    <link rel="stylesheet" href="/resources/layui_ext/dtree/dtree.css" media="all" />
    <link rel="stylesheet" href="/resources/layui_ext/dtree/font/dtreefont.css" media="all" />
</head>
<body class="childrenBody">
<!-- 查询条件开始-->

<form class="layui-form">
    <blockquote class="layui-elem-quote quoteBox">
        <form class="layui-form" >
            <div class="layui-inline">
                 <label class="layui-form-label">菜单名称</label>
				<div class="layui-input-inline">
					<input type="text" name="title" id="title" class="layui-input searchVal" placeholder="请输入菜单名称" />
				</div>
                <button type="button" class="layui-btn" lay-submit="" lay-filter="doSearch"><span class="layui-icon layui-icon-search"></span>查询</button>
                <button type="reset" class="layui-btn layui-btn-warm"><span class="layui-icon layui-icon-refresh-1"></span>重置</button>
            </div>
        </form>
    </blockquote>
</form>
<!-- 查询条件结束-->


<!-- 数据表格开始 -->
<div>
    <table class="layui-hide" id="menuTable" lay-filter="menuTable"></table>
    <!-- 工具栏 -->
    <div id="menuToolBar" style="display: none;">
        <button type="button" class="layui-btn layui-btn-sm" lay-event="add"><span class="layui-icon layui-icon-add-1"></span>添加菜单</button>
    </div>

    <div id="menuRowBar" style="display: none;">
        <button type="button"  class="layui-btn layui-btn-sm"lay-event="update"><span class="layui-icon layui-icon-edit"></span>更新</button>
        <button type="button" class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delete"><span class="layui-icon layui-icon-delete"></span>删除</button>
    </div>
</div>
<!-- 数据表格结束 -->

<!-- 添加和修改的弹出层开始 -->

<div  style="display: none;padding: 5px" id="addOrUpdateDiv">
    <form class="layui-form" style="width:90%;" id="dataFrm" lay-filter="dataFrm">
        <div class="layui-form-item">
            <label class="layui-form-label">父级菜单</label>
            <input type="hidden" name="pid" id="pid">
            <div class="layui-input-block">
                <ul id="menuTree" class="dtree" data-id="0"></ul>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">菜单名称</label>
            <div class="layui-input-block">
                <input type="hidden" name="id">
                <input type="text" name="title" lay-verify="required" autocomplete="off" placeholder="请输入菜单名称" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">菜单地址</label>
            <div class="layui-input-block">
                <input type="text" name="href"  autocomplete="off" placeholder="请输入菜单地址" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">菜单图标</label>
                <div class="layui-input-block">
                    <input type="text" name="icon"  autocomplete="off" placeholder="请输入菜单图标" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">是否展开</label>
                <div class="layui-input-block">
                    <input type="radio" name="open" value="1" title="是" checked="">
                    <input type="radio" name="open" value="0" title="否" >
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">是否可用</label>
                <div class="layui-input-block">
                    <input type="radio" name="available" value="1" title="是" checked="">
                    <input type="radio" name="available" value="0" title="否" >
                </div>
            </div>
        </div>

        <div class="layui-form-item layui-row layui-col-xs12">
            <div class="layui-input-block" style="text-align: center;">
                <button type="button" class="layui-btn" lay-submit lay-filter="doSubmit"><span class="layui-icon layui-icon-add-1"></span>提交</button>
                <button type="reset" class="layui-btn layui-btn-warm"><span class="layui-icon layui-icon-refresh-1"></span>重置</button>
            </div>
        </div>
    </form>
</div>

<!-- 添加和修改的弹出层结束 -->




<script type="text/javascript" src="/resources/layui/layui.js"></script>
<script type="text/javascript">

    var tableIns;
    layui.extend({
        dtree: '/resources/layui_ext/dtree/dtree'   // {/}的意思即代表采用自有路径，即不跟随 base 路径
    }).use(['jquery','form','table','layer','dtree'],function () {
        var $=layui.jquery;
        var form=layui.form;
        var table=layui.table;
        var layer=layui.layer;
        var dtree = layui.dtree;

        //表格渲染
        tableIns=table.render({
            elem: '#menuTable'
                ,url:'/sys/menu/list'
            ,toolbar: '#menuToolBar' //开启头部工具栏，并为其绑定左侧模板
            ,page: true
            ,title: '菜单数据表'
            ,cols: [  [
                {type:'checkbox'}
                ,{field:'id',title:'菜单ID',align:'center'}
                ,{field:'pid',title:'父级菜单ID',align:'center'}
                ,{field:'title',title:'菜单名称',align:'center'}
                ,{field:'href',title:'菜单地址',align:'center'}
                ,{field:'open',title:'是否展开',align:'center',templet:function (d) {
                        return d.open==1?"<font color='blue'>是</font>":"<font color='red'>否</font>";
                    }}
                ,{title:'操作',toolbar:'#menuRowBar'}
            ]  ],
            done:function (res,curr,count) {
                //判断当前页面是否大于1并且当前页的数据为0
                if (curr>1&&res.data.length==0){
                    var pageValue=curr-1
                    //刷新数据表格
                    tableIns.reload({
                        page:{curr:pageValue}
                    });
                }
            }
        });

        //监听表单提交事件
        form.on("submit(doSearch)",function (data) {
            tableIns.reload({
                url:'/sys/menu/list',
                where:data.field,
                page:{
                    curr:1
                }
            });
            return false;
        });

        //监听头部工具栏事件
        table.on('toolbar(menuTable)',function (obj) {
            switch (obj.event) {
                case 'add':
                    //打开头部工具栏添加菜单的窗口
                    openAddMenu();
                    break;
            }
        })




        /**
         * 添加菜单
         */
        function openAddMenu(){
            mainIndex= layer.open({
                type:1,
                content:$('#addOrUpdateDiv'),
                area:["800px","500px"],
                title:"添加菜单",
                success:function () {
                    //每次打开窗口，清空表单数据
                    $("#dataFrm")[0].reset();
                    //提交数据
                    url="/sys/menu/addMenu";
                }

            })
        }

        /**
         * 添加菜单父级下拉框需要在页面一开始加载就需要搞，不要在打开窗口时候再加载，不然加载不过来
         */
        var menuSelectTree=dtree.renderSelect({
            elem: "#menuTree",
            width: "100%", // 指定树的宽度
            url: "/sys/menu/loadMenuTreeLeft", // 使用url加载（可与data加载同时存在）
            dataStyle: "layuiStyle",  //使用layui风格的数据格式
            dataFormat: "list",  //配置data的风格为list
            response:{message:"msg",statusCode:0},  //修改response中返回数据的定义
        });

        //监听添加下拉框树节点的点击事件id
        // 绑定节点点击
        dtree.on("node('menuTree')" ,function(obj){
            //给父级部门隐藏域赋值
            $("#pid").val(obj.param.nodeId);

        });
        var mainIndex;//打开窗口的索引
        var url;//提交请求的地址

        //监听表单提交事件
        form.on("submit(doSubmit)",function (data) {
            $.post(url, data.field, function (result) {
                if (result.success){
                    //提示信息
                    layer.msg(result.message,{icon:1})
                    tableIns.reload();//重新加载刷新数据表单表单
                    //刷新下拉菜单树，避免添加成功后父级菜单树没有刷新
                    menuSelectTree.reload();
                    //刷新左侧部门管理菜单树
                    window.parent.left.reload();

                    //关闭当前窗口
                    layer.close(mainIndex);

                }else {
                    layer.msg(result.message,{icon:2});
                }
                //关闭当前窗口
                layer.close(mainIndex);

            }, "json");
            //禁止页面跳转
            return false;
        })


        //监听行工具栏事件
        table.on('tool(menuTable)',function (obj) {
            switch (obj.event) {
                case 'update':
                    //打开头部工具栏添加菜单的窗口
                    openUpdateMenu(obj.data);
                    break;
                case 'delete':
                    deleteMenu(obj.data);
                    break
            }
        })

        /**
         * 打开修改菜单
         */
        function  openUpdateMenu(data){
            mainIndex= layer.open({
                type:1,
                content:$('#addOrUpdateDiv'),
                area:["800px","500px"],
                title:"修改菜单",
                success:function () {
                    //表单数据回显
                    form.val("dataFrm",data);
                    //下拉列表回显
                    dtree.dataInit("menuTree",data.pid)
                    //设置显示值
                    dtree.selectVal("menuTree");
                    //提交数据
                    url="/sys/menu/updateMenu";
                }

            })
        }

        /**
         * 删除
         */
        function deleteMenu(data){
            $.post("/sys/menu/checkMenuHasChild", {"id":data.id}, function (result) {
                if(result.exist){
                    layer.msg("对不起，该菜单下有子菜单,无法删除")
                }else {
                    layer.confirm('是否要删除该数据？', {"icon":3,"title":"提示"},function (index) {
                        $.post("/sys/menu/deleteById", {"id":data.id}, function (result) {
                            console.log(result);
                            if (result.success){
                                tableIns.reload();//表格重新加载
                                //刷新左侧下拉树
                                menuSelectTree.reload();
                                //刷新左侧菜单栏
                                window.parent.left.reload();
                            }
                            layer.msg(result.message);
                        }, "json");
                        layer.close(index);
                    });
                }
            }, "json");
        }


    });


    /**
     * 根据菜单ID点击后加载部门列表
     * @param deptId
     */
    function loadTableData(menuId){
        tableIns.reload({
            where:{"id":menuId},
            page:{
                curr:1
            }
        });//重载表格刷新
    }

    
</script>
</body>
</html>